package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruibang.glass.produce.domain.req.DcsRealTimeDataReq;
import com.ruibang.glass.produce.domain.resp.dcs.cx.*;
import com.ruibang.glass.produce.mapper.DcsCxRealTimeDataMapper;
import com.ruibang.glass.produce.service.DcsCxRealTimeDataService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-04-23  14:57
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
public class DcsCxRealTimeDataServiceImpl implements DcsCxRealTimeDataService {


    @Autowired
    private DcsCxRealTimeDataMapper dcsCxRealTimeDataMapper;

    /**
     * @Description: 查询L管马弗炉功率温度
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-26 9:26
     */
    @Override
    public IPage<PowerTemp> queryPowerTemp(DcsRealTimeDataReq dcsRealTimeDataReq) {
        setDefaultTime(dcsRealTimeDataReq);
        return dcsCxRealTimeDataMapper.queryPowerTemp(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }

    /**
     * @Description: 查询L管马弗炉功率温度
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-26 9:26
     */
    @Override
    public IPage<MufflePowerTemp> queryMufflePowerTemp(DcsRealTimeDataReq dcsRealTimeDataReq) {
        setDefaultTime(dcsRealTimeDataReq);
        return dcsCxRealTimeDataMapper.queryMufflePowerTemp(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }

    /**
     * @Description: 查询一区功率温度
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-26 9:26
     */
    @Override
    public IPage<ZoneOnePowerTemp> queryZoneOnePowerTemp(DcsRealTimeDataReq dcsRealTimeDataReq) {
        setDefaultTime(dcsRealTimeDataReq);
        return dcsCxRealTimeDataMapper.queryZoneOnePowerTemp(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }

    /**
     * @Description: 查询2-5区功率
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-26 9:26
     */
    @Override
    public IPage<ZoneTwoToFivePower> queryZoneTwoToFivePower(DcsRealTimeDataReq dcsRealTimeDataReq) {
        setDefaultTime(dcsRealTimeDataReq);
        return dcsCxRealTimeDataMapper.queryZoneTwoToFivePower(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }

    /**
     * @Description: 查询2-3区温度
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-26 9:26
     */
    @Override
    public IPage<ZoneTwoToThreeTemp> queryZoneTwoToThreeTemp(DcsRealTimeDataReq dcsRealTimeDataReq) {
        setDefaultTime(dcsRealTimeDataReq);
        return dcsCxRealTimeDataMapper.queryZoneTwoToThreeTemp(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }

    /**
     * @Description: 查询4-5区温度
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-26 9:26
     */
    @Override
    public IPage<ZoneFourToFiveTemp> queryZoneFourToFiveTemp(DcsRealTimeDataReq dcsRealTimeDataReq) {
        setDefaultTime(dcsRealTimeDataReq);
        return dcsCxRealTimeDataMapper.queryZoneFourToFiveTemp(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }

    /**
     * @Description: 查询冷却水温度流量
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-26 9:26
     */
    @Override
    public IPage<WaterTempFlow> queryWaterTempFlow(DcsRealTimeDataReq dcsRealTimeDataReq) {
        setDefaultTime(dcsRealTimeDataReq);
        return dcsCxRealTimeDataMapper.queryWaterTempFlow(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }

    /**
     * @Description: 查询应力区功率
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-26 9:26
     */
    @Override
    public IPage<StressPower> queryStressPower(DcsRealTimeDataReq dcsRealTimeDataReq) {
        setDefaultTime(dcsRealTimeDataReq);
        return dcsCxRealTimeDataMapper.queryStressPower(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }

    @Override
    public void exportPowerTemp(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<PowerTemp> page = this.queryPowerTemp(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), PowerTemp.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportMufflePowerTemp(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<MufflePowerTemp> page = this.queryMufflePowerTemp(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), MufflePowerTemp.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportZoneOnePowerTemp(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<ZoneOnePowerTemp> page = this.queryZoneOnePowerTemp(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), ZoneOnePowerTemp.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportZoneTwoToFivePower(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<ZoneTwoToFivePower> page = this.queryZoneTwoToFivePower(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), ZoneTwoToFivePower.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportZoneTwoToThreeTemp(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<ZoneTwoToThreeTemp> page = this.queryZoneTwoToThreeTemp(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), ZoneTwoToThreeTemp.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportZoneFourToFiveTemp(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<ZoneFourToFiveTemp> page = this.queryZoneFourToFiveTemp(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), ZoneFourToFiveTemp.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportWaterTempFlow(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<WaterTempFlow> page = this.queryWaterTempFlow(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), WaterTempFlow.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportStressPower(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<StressPower> page = this.queryStressPower(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), StressPower.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public MufflePowerTempTrendChart mufflePowerTempTrendChart(DcsRealTimeDataReq dcsRealTimeDataReq) {
        //默认时间
        if (dcsRealTimeDataReq.getStartTime() == null || dcsRealTimeDataReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusDays(7);
            dcsRealTimeDataReq.setStartTime(startTime);
            dcsRealTimeDataReq.setEndTime(endTime);
        }
        List<MufflePowerTemp> mufflePowerTempList = dcsCxRealTimeDataMapper.mufflePowerTempTrendChart(dcsRealTimeDataReq);
        MufflePowerTempTrendChart mufflePowerTempTrendChart = new MufflePowerTempTrendChart();
        if (CollectionUtils.isNotEmpty(mufflePowerTempList)) {
            //采集时间
            List<String> collectTime = mufflePowerTempList.stream().map(MufflePowerTemp::getRe2CXCXCx01PCXFZ2PTIMESTAMP).collect(Collectors.toList());
            mufflePowerTempTrendChart.setCollectTime(collectTime);

            //TMLL2温度 Re2CXCXCx01TC16AMONVALUE
            List<Double> re2CXCXCx01TC16AMONVALUE = mufflePowerTempList.stream().map(MufflePowerTemp::getRe2CXCXCx01TC16AMONVALUE).collect(Collectors.toList());
            mufflePowerTempTrendChart.setRe2CXCXCx01TC16AMONVALUE(re2CXCXCx01TC16AMONVALUE);

            //TOP2温度  Re2CXCXCx02TC93AMONVALUE
            List<Double> re2CXCXCx02TC93AMONVALUE = mufflePowerTempList.stream().map(MufflePowerTemp::getRe2CXCXCx02TC93AMONVALUE).collect(Collectors.toList());
            mufflePowerTempTrendChart.setRe2CXCXCx02TC93AMONVALUE(re2CXCXCx02TC93AMONVALUE);

            //TMLR2温度 Re2CXCXCx02TC62AMONVALUE
            List<Double> re2CXCXCx02TC62AMONVALUE = mufflePowerTempList.stream().map(MufflePowerTemp::getRe2CXCXCx02TC62AMONVALUE).collect(Collectors.toList());
            mufflePowerTempTrendChart.setRe2CXCXCx02TC62AMONVALUE(re2CXCXCx02TC62AMONVALUE);

            //YLIR温度  Re2CXCXCx02TC64AMONVALUE
            List<Double> re2CXCXCx02TC64AMONVALUE = mufflePowerTempList.stream().map(MufflePowerTemp::getRe2CXCXCx02TC64AMONVALUE).collect(Collectors.toList());
            mufflePowerTempTrendChart.setRe2CXCXCx02TC64AMONVALUE(re2CXCXCx02TC64AMONVALUE);

            //YLER温度  Re2CXCXCx02TC65AMONVALUE
            List<Double> re2CXCXCx02TC65AMONVALUE = mufflePowerTempList.stream().map(MufflePowerTemp::getRe2CXCXCx02TC65AMONVALUE).collect(Collectors.toList());
            mufflePowerTempTrendChart.setRe2CXCXCx02TC65AMONVALUE(re2CXCXCx02TC65AMONVALUE);
        }
        return mufflePowerTempTrendChart;
    }


    @Nullable
    private void setDefaultTime(DcsRealTimeDataReq dcsRealTimeDataReq) {
        if (dcsRealTimeDataReq.getStartTime() == null || dcsRealTimeDataReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusDays(7);
            dcsRealTimeDataReq.setStartTime(startTime);
            dcsRealTimeDataReq.setEndTime(endTime);
        }
    }
}
